package com.im.app.controller;

import com.im.app.authConfig.CurrentUser;
import com.im.app.authConfig.RequireLogin;
import com.im.app.manager.ChatLogManager;
import com.im.app.manager.ConversationManager;
import com.im.app.model.Conversations;
import com.im.app.model.User;
import com.im.app.service.ChatLogsService;
import com.im.app.service.ConversationsService;
import com.im.app.service.IUserService;
import com.im.app.utils.Response;
import com.im.app.utils.ResponseResult;
import org.jim.core.packets.ChatType;
import org.jim.server.util.ChatKit;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

import java.util.List;

/**
 * 会话控制器
 */
@RestController
@RequestMapping(value = "/conversation")
public class IMConversationController extends BaseController{

    @Resource
    private ConversationsService conversationsService;
    @Resource
    private ChatLogsService chatLogsService;

    @Resource
    private IUserService userService;


    /**
     * 根据备注昵称询会话
     * @return
     */
    @RequireLogin
    @GetMapping("/getConversationByKeyWord")
    public ResponseResult<Object> getConversationByKeyWord(@CurrentUser User user,String keyWord){
        List<Conversations> conversationsList = conversationsService.getConversationByKeyWord(user.getUserId(),keyWord);
        for (Conversations conversation: conversationsList) {
            if (conversation.getConversationType() == ChatType.CHAT_TYPE_PRIVATE.getNumber()){
                conversation.setOnLine(ChatKit.isOnline(conversation.getUserId(),false));
            }
        }
        return Response.makeOKRsp().setData(conversationsList);
    }

    /**
     * 获取所有会话
     * @return
     */
    @RequireLogin
    @GetMapping("/getConversation")
    public ResponseResult<Object> getConversation(@CurrentUser User user){
        List<Conversations> conversationsList = conversationsService.getConversationsList(user.getUserId());
        for (Conversations conversation: conversationsList) {
            if (conversation.getConversationType() == ChatType.CHAT_TYPE_PRIVATE.getNumber()){
                conversation.setOnLine(ChatKit.isOnline(conversation.getUserId(),false));
            }
        }
        return Response.makeOKRsp().setData(conversationsList);
    }

    @RequireLogin
    @GetMapping("/createConversation")
    public ResponseResult<Object> createConversation(@CurrentUser User user, Integer type, String id){
        if(type == null){
            return Response.makeErrRsp("会话类型不能为空");
        }
        if(id == null){
            return Response.makeErrRsp("会话ID不能为空");
        }
        return conversationsService.createConversation(user.getUserId(), type,id);
    }

    /**
     * 清除会话
     * */
    @RequireLogin
    @GetMapping("/clearConversation")
    public ResponseResult<Object> clearConversation(String conversationId){
        int result = conversationsService.clearConversations(conversationId);
        if(result>0){
            return Response.makeOKRsp("操作成功");
        }
        return Response.makeErrRsp("操作失败");
    }

    /**
     * 清除会话 n天前
     * */
    @RequireLogin
    @GetMapping("/clearByDays")
    public ResponseResult<Object> clearByDays(@CurrentUser User user,Integer days){
        int result = conversationsService.clearByDays(user.getUserId(),days);
        return Response.makeOKRsp("操作成功").setData("成功删除了"+result+"条会话");
    }

    /**
     * 清除会话 n天前(外服务调用)
     * */
    @GetMapping("/clearByDaysOut")
    public int clearByDaysOut(Integer days){
        return conversationsService.clearByDaysOut(days);
    }


    /**
     * 清除会话 ids(外服务调用)
     * */
    @GetMapping("/clearByUserIdsOut")
    public int clearByUserIdsOut(String ids){
        return conversationsService.clearByUserIdsOut(ids);
    }



    /**
     * 会话置顶
     * */
    @RequireLogin
    @GetMapping ("/updateIsPinned")
    public ResponseResult<Object> updateIsPinned(String conversationId, Integer isPinned){
        int result = conversationsService.updateIsPinned(isPinned,conversationId);
        if(result>0){
            Conversations byId = conversationsService.getById(conversationId);
            ConversationManager.getManager().sendUserChange(byId);
            return Response.makeOKRsp("操作成功");
        }
        return Response.makeErrRsp("操作失败");
    }

    /**
     * 修改备注昵称
     * */
    @RequireLogin
    @PostMapping("/updateNick")
    public ResponseResult<Object> updateNick(String conversationId, String nickName){
        int result = conversationsService.updateNick(nickName,conversationId);
        if(result>0){
            Conversations byId = conversationsService.getById(conversationId);
            ConversationManager.getManager().sendUserChange(byId);
            return Response.makeOKRsp("操作成功");
        }
        return Response.makeErrRsp("操作失败");
    }

    /**
     * 清除未读数
     * */
    @RequireLogin
    @GetMapping("/clearUnreadCount")
    public ResponseResult<Object> clearUnreadCount(@CurrentUser User user, String conversationId, String fromUserId) {
        int result = conversationsService.clearUnreadCount(conversationId);
        //消息已读
        chatLogsService.updateIsRead(fromUserId,user.getUserId());
        Conversations conversationB = conversationsService.selectByUser(fromUserId,user.getUserId());
        if(conversationB != null){
            ChatLogManager.sendChatLogIsRead(conversationB.getConversationId(),fromUserId);
        }

        return Response.makeOKRsp("操作成功");
    }

    @GetMapping("/getTotalMember")
    public ResponseResult<Object> getTotalMember() {
        return Response.makeOKRsp("操作成功").setData(ConversationManager.getManager().totalUser());
    }

}
